﻿// CTestDlg.cpp: 实现文件
//

#include "pch.h"
#include "Mybole.h"
#include "CTestDlg.h"
#include "afxdialogex.h"
#include "resource.h"


// CTestDlg 对话框

IMPLEMENT_DYNAMIC(CTestDlg, CDialog)

CTestDlg::CTestDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_CTestDlg, pParent)
	, m_num1(0)
	, m_num2(0)
	, m_num3(0)
{
	m_btnCreate = false;
}

CTestDlg::~CTestDlg()
{
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//  DDX_Text(pDX, IDC_EDIT1, m_num1); // 变量和控件进行关联
	DDX_Text(pDX, IDC_EDIT2, m_num2);
	DDX_Text(pDX, IDC_EDIT3, m_num3);
	DDX_Text(pDX, IDC_EDIT1, m_num1);
	DDV_MinMaxInt(pDX, m_num1, 0, 100); // 设置最大值最小值
	DDV_MinMaxInt(pDX, m_num2, 0, 100);
	DDX_Control(pDX, IDC_EDIT1, m_edit1);
	DDX_Control(pDX, IDC_EDIT3, m_edit3);
	DDX_Control(pDX, IDC_EDIT2, m_edit2);
}

void CTestDlg::OnOK()
{
	//CDialog::OnOK();
	// GetDlgItem(IDC_EDIT1)->GetNextWindow(GW_HWNDNEXT)->SetFocus();
	//GetFocus()->GetNextWindow()->SetFocus();
	//GetFocus()->GetWindow(GW_HWNDNEXT)->SetFocus();
	GetNextDlgTabItem(GetFocus(), false)->SetFocus();

	//this->DestroyWindow(); // 销毁非模态对话框
}


BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	ON_BN_CLICKED(IDC_BTN_ADD, &CTestDlg::OnBtnAdd)
	ON_STN_CLICKED(IDC_NUMBER1, &CTestDlg::OnClickedNumber1)
	ON_BN_CLICKED(IDC_BTN_SC, &CTestDlg::OnBnClickedBtnSc)
	ON_BN_CLICKED(IDOK, &CTestDlg::OnOK)
END_MESSAGE_MAP()


// CTestDlg 消息处理程序
void CTestDlg::OnBtnAdd()
{
	// TODO: 在此添加控件通知处理程序代码
	/*
	* 动态创建按钮
	//static bool btnCreate = false; 这种方式也可以
	if (m_btnCreate == false) {
		m_btn.Create("添加按钮", BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD, CRect(0, 0, 100, 100), this, 120);
		m_btnCreate = true;
	}
	else {
		m_btn.DestroyWindow();
		m_btnCreate = false;
		//this->OnBtnAdd();
	}
	*/

	/*
	if (!m_btn.m_hWnd) {  // 只有创建了窗口句柄才会有值
		m_btn.Create("添加按钮", BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD, CRect(0, 0, 100, 100), this, 120);
	}
	else {
		m_btn.DestroyWindow();
	}
	*/

	/*
	// 两个数值相加   方法一：
	CString str1, str2;
	GetDlgItem(IDC_EDIT1)->GetWindowText(str1);
	GetDlgItem(IDC_EDIT2)->GetWindowText(str2);
	CString str;
	str.Format("%d", _ttoi(str1) + _ttoi(str2));
	GetDlgItem(IDC_EDIT3)->SetWindowText(str);
	*/

	/* 两个数值相加   方法二：
	int num1, num2, num3;
	char ch1[10], ch2[10], ch3[10];

	GetDlgItemText(IDC_EDIT1, ch1, 10);
	GetDlgItemText(IDC_EDIT2, ch2, 10);
	GetDlgItemText(IDC_EDIT3, ch3, 10);

	num1 = atoi(ch1);
	num2 = atoi(ch2);
	num3 = num1 + num2;
	_itoa_s(num3, ch3, 10); // 数值和字符串的转换
	SetDlgItemText(IDC_EDIT3, ch3);
	*/

	/*
	两个数值相加   方法三：
	int num1, num2, num3;
	num1 = GetDlgItemInt(IDC_EDIT1);
	num2 = GetDlgItemInt(IDC_EDIT2);

	num3 = num1 + num2;
	SetDlgItemInt(IDC_EDIT3, num3);
	*/

	//两个数值相加   方法四：
	/*
	UpdateData(true);
	m_num3 = m_num1 + m_num2;
	UpdateData(false);
	*/

	//两个数值相加   方法五：
	/*
	int num1, num2, num3;
	char ch1[10], ch2[10], ch3[10];
	m_edit1.GetWindowText(ch1, 10);
	m_edit1.GetWindowText(ch2, 10);
	num1 = atoi(ch1);
	num2 = atoi(ch2);
	num3 = num1 + num2;
	_itoa_s(num3, ch3, 10); // 数值和字符串的转换
	SetDlgItemText(IDC_EDIT3, ch3);
	*/

	/*两个数值相加   方法六：
	int num1, num2, num3;
	char ch1[10], ch2[10], ch3[10];
	// 发送消息的四种方式
	::SendMessage(GetDlgItem(IDC_EDIT1)->m_hWnd, WM_GETTEXT, 10, (LPARAM)ch1);
	//::SendMessage(m_edit2.m_hWnd, WM_GETTEXT, 10, (LPARAM)ch2);
	m_edit2.SendMessage(WM_GETTEXT, 10, (LPARAM)ch2);
	num1 = atoi(ch1);
	num2 = atoi(ch2);
	num3 = num1 + num2;
	_itoa_s(num3, ch3, 10); // 数值和字符串的转换
	m_edit3.SendMessage(WM_SETTEXT, 0, (LPARAM)ch3);
	*/

	int num1, num2, num3;
	char ch1[10], ch2[10], ch3[10];
	SendDlgItemMessage(IDC_EDIT1, WM_GETTEXT, 10, (LPARAM)ch1);
	SendDlgItemMessage(IDC_EDIT2, WM_GETTEXT, 10, (LPARAM)ch2);
	num1 = atoi(ch1);
	num2 = atoi(ch2);
	num3 = num1 + num2;
	_itoa_s(num3, ch3, 10); // 数值和字符串的转换
	m_edit3.SendMessage(WM_SETTEXT, 0, (LPARAM)ch3);
	SendDlgItemMessage(IDC_EDIT3, WM_SETTEXT, 0, (LPARAM)ch3);

	SendDlgItemMessage(IDC_EDIT3, EM_SETSEL, 0, -1);// 设置选择
	m_edit3.SetFocus();// 设置焦点
}


void CTestDlg::OnClickedNumber1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str;
	if (GetDlgItem(IDC_NUMBER1)->GetWindowText(str), str == "Number1:") {
		GetDlgItem(IDC_NUMBER1)->SetWindowText("数值1：");
	}
	else {
		GetDlgItem(IDC_NUMBER1)->SetWindowText("Number1:");
	}
}



void CTestDlg::OnBnClickedBtnSc()
{
	// TODO: 在此添加控件通知处理程序代码
	CString str;
	if (GetDlgItemText(IDC_BTN_SC, str), str == "收缩<<") {
		SetDlgItemText(IDC_BTN_SC, "拓展>>");
	}
	else {
		SetDlgItemText(IDC_BTN_SC, "收缩<<");
	}

	static CRect rectLarge;
	static CRect rectSmall;

	if (rectLarge.IsRectNull()) {
		CRect rectSeparator;
		GetWindowRect(&rectLarge);

		GetDlgItem(IDC_SEPARATOR)->GetWindowRect(&rectSeparator);

		rectSmall.left = rectLarge.left;
		rectSmall.top = rectLarge.top;
		rectSmall.right = rectLarge.right;
		rectSmall.bottom = rectSeparator.bottom;
	}

	if (str == "收缩<<") {
		SetWindowPos(NULL, 0, 0, rectSmall.Width(), rectSmall.Height(), SWP_NOMOVE | SWP_NOZORDER);
	}
	else {
		SetWindowPos(NULL, 0, 0, rectLarge.Width(), rectLarge.Height(), SWP_NOMOVE | SWP_NOZORDER);

	}
}

// 窗口的聚焦切换
WNDPROC prevProc;
LRESULT CALLBACK WinSunProc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam)
{
	if (uMsg == WM_CHAR && wParam == 0x0d) {
		//::SetFocus(::GetNextWindow(hwnd, GW_HWNDNEXT));
		//SetFocus(::GetWindow(hwnd, GW_HWNDNEXT));
		SetFocus(::GetNextDlgTabItem(::GetParent(hwnd), hwnd, false));
		return 1;
	}
	else {
		return prevProc(hwnd, uMsg, wParam, lParam);
	}
}


BOOL CTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化
	prevProc = (WNDPROC)SetWindowLong(GetDlgItem(IDC_EDIT1)->m_hWnd, GWL_WNDPROC, (LONG)WinSunProc);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}
